package net.form.processing;

import java.awt.Color;

public class Invert extends AbstractImageCommand {

	private static int blanco = (new Color(255, 255, 255)).getRGB();

	private static int negro = (new Color(0, 0, 0)).getRGB();

	public Invert(Image image) {
		super(image);
	}

	/**
	 * Invierte la imagen de negro a blanco, y el resto a negro
	 * 
	 * @param image
	 * @return
	 */
	private Image invertBinaryImage(Image image) {
		if (image != null) {
			for (int x = 0; x < image.getWidth(); x++)
				for (int y = 0; y < image.getHeight(); y++) {
					try {
						if (image.getRGB(x, y) == negro) {
							image.setRGB(x, y, blanco);
						} else {
							image.setRGB(x, y, negro);
						}
					} catch (Exception e) {
						System.out.println("x: " + x + ", y: " + y);
						e.printStackTrace();
						return null;
					}
				}
			return image;
		}
		return null;
	}

	@Override
	public Image execute() {
		return invertBinaryImage(getImage());
	}

	@Override
	public String getCommandName() {
		return "Invertir imagen binaria";
	}

}
